package com.luxk.db.storage.impl;

import java.util.HashMap;

import com.luxk.db.common.ImpactException;
import com.luxk.db.common.ModuleBase;
import com.luxk.db.server.ShutdownMode;
import com.luxk.db.storage.Storage;
import com.luxk.db.storage.StorageGroup;
import com.luxk.db.tests.UnitTest;

public class DefaultStorage extends ModuleBase
implements Storage {
	//private static final int MAX_STORAGE_COUNT = 64;
	public static final String PATH_PREFIX = "+/";
	public static final String PATH_SEPARATOR = "/";
	
	private HashMap<String, DefaultStorageGroup> groups =
			new HashMap<String, DefaultStorageGroup>();
	
	public String getPathPrefix() {
		return PATH_PREFIX;
	}
	
	public String getPathSeparator() {
		return PATH_SEPARATOR;
	}

	public void mount(String[] paths) throws ImpactException {
		
		// acqiure lock
		
		try {
			for(int i = 0; i < paths.length; i++) {
				String[] temp = new String[] { paths[i] };
				DefaultStorageGroup group = new DefaultStorageGroup();
				group.mount(temp);
				this.groups.put(group.getName(), group);
			}
			
		} catch(ImpactException e) {
			// 모든 StorageGroup을 unmount
			StorageGroup[] arr =
					(StorageGroup[])(this.groups.values().toArray());
			for(StorageGroup ee: arr) {
				try { ee.unmount(); } catch(ImpactException eee) {}
			}
			this.groups.clear();
			
			throw e;
		}
		
		// release lock
	}

	public void recovery() throws ImpactException {
		// do nothing
	}

	public void unmount() throws ImpactException {
		// close all files
		StorageGroup[] arr =
				(StorageGroup[])(this.groups.values().toArray());
		for(StorageGroup ee: arr) {
			try { ee.unmount(); } catch(ImpactException eee) {}
		}
		this.groups.clear();
	}

	public StorageGroup createStorageGroup(Object param) throws ImpactException {
		// acquire lock
		
		// create
		ParamCreateStorageGroup cGrpParam = (ParamCreateStorageGroup)param;
		DefaultStorageGroup group = new DefaultStorageGroup();
		group.create(cGrpParam.name, cGrpParam.parentPath);
		this.groups.put(cGrpParam.name, group);
		
		// release lock
		
		return group;
	}

	public void dropStorageGroup(String name) throws ImpactException {
		// acquire lock
		
		// drop
		DefaultStorageGroup group = this.groups.get(name);
		group.drop();
		
		// release lock
	}

	public StorageGroup getStorageGroup(String name) {
		return this.groups.get(name);
	}
	
	public StorageGroup[] getStorageGroupList() throws ImpactException {
		return (StorageGroup[])(this.groups.values().toArray());
	}
	
	public void init() {
	}

	public void raiseNomount() throws Exception {}
	public void raiseMount() throws Exception {}
	public void raiseStandby() throws Exception {}
	public void raiseNormal() throws Exception {}
	
	public void cancelNomount() throws Exception {}
	public void cancelMount() throws Exception {}
	public void cancelStandby() throws Exception {}
	public void cancelNormal() throws Exception {}

	public void shutdown(ShutdownMode mode) throws Exception {
	}
	
	public UnitTest getUnitTest() {
		return new StorageTest();
	}
}
